package com.mflex.mwms.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.reflect.TypeToken;
import com.mflex.mwms.R;
import com.mflex.mwms.adapter.JlrCompareDetailAdapter;
import com.mflex.mwms.adapter.TeslaCompareDetailAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.JLRPalletBox;
import com.mflex.mwms.model.Task;
import com.mflex.mwms.model.TaskDetail;
import com.mflex.mwms.model.TeslaComapreInfo;
import com.mflex.mwms.model.TeslaCustomCompareRequest;
import com.mflex.mwms.model.enumeration.CustomCompare;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class JlrCompareDetailActivity extends BaseActivity {
    @BindView(R.id.stv_CustomPallet)
    EditText stvCustomPallet;

    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;
    @BindView(R.id.stv_CustomBoxNumber)
    EditText stvCustomBoxNumber;
    @BindView(R.id.button_clear)
    Button buttonClear;

    @BindView(R.id.button_submit)
    Button buttonSubmit;

    @BindView(R.id.lvTaskList)
    ListView lvTaskList;

    MiniLoadingDialog mMiniLoadingDialog;

    List<JLRPalletBox> jlrPalletBoxList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jlr_compare_detail);
        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");


        stvBoxNumber.setOnKeyListener(this);
        stvCustomBoxNumber.setOnKeyListener(this);
        buttonClear.setOnClickListener(this);
        buttonSubmit.setOnClickListener(this);

        jlrPalletBoxList = new ArrayList<>();

        stvBoxNumber.postDelayed(new Runnable() {
            @Override
            public void run() {
                stvBoxNumber.requestFocus();
            }
        }, 200);

        Intent intent = getIntent();
        Type listType = new TypeToken<List<JLRPalletBox>>() {
        }.getType();
        jlrPalletBoxList = (List<JLRPalletBox>) JsonUtil.fromJson(intent.getStringExtra("JLRPALLETBOX"), listType);

        stvCustomPallet.setText(jlrPalletBoxList.get(0).getCustomPallet());

        JlrCompareDetailAdapter jlrCompareDetailAdapter = new JlrCompareDetailAdapter(JlrCompareDetailActivity.this, jlrPalletBoxList);
        lvTaskList.setAdapter(jlrCompareDetailAdapter);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;
            case R.id.button_submit:
                SubmitButtonMd(view);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_BoxNumber:
                    checkBoxNumber(view);
                    break;
                case R.id.stv_CustomBoxNumber:
                    checkCustomBoxNumber(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void checkBoxNumber(View view) {
        if (StringUtils.isEmpty(stvBoxNumber.getText().toString())) {
            return;
        }
        boolean flag = false;
        for (JLRPalletBox jlrPalletBox : jlrPalletBoxList) {
            if (StringUtils.equals(jlrPalletBox.getBoxNumber(), stvBoxNumber.getText().toString())) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            stvBoxNumber.setText("");
            stvBoxNumber.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stvBoxNumber.requestFocus();
                }
            }, 200);
            MessageUtils.errorMsgAndVoice(JlrCompareDetailActivity.this, "箱不在列表中");
            return;
        }
        boolean flag2 = false;
        for (JLRPalletBox jlrPalletBox : jlrPalletBoxList) {
            if (StringUtils.equals(jlrPalletBox.getBoxNumber(), stvBoxNumber.getText().toString()) && jlrPalletBox.isChecked()) {
                flag2 = true;
                break;
            }
        }
        if (flag2) {
            stvBoxNumber.setText("");
            stvBoxNumber.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stvBoxNumber.requestFocus();
                }
            }, 200);
            MessageUtils.errorMsgAndVoice(JlrCompareDetailActivity.this, "箱已比对");
            return;
        }
        stvCustomBoxNumber.postDelayed(new Runnable() {
            @Override
            public void run() {
                stvCustomBoxNumber.requestFocus();
            }
        }, 200);

    }

    private void checkCustomBoxNumber(View view) {
        if (StringUtils.isEmpty(stvBoxNumber.getText().toString())) {
            stvCustomBoxNumber.setText("");
            stvBoxNumber.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stvBoxNumber.requestFocus();
                }
            }, 200);

            MessageUtils.errorMsgAndVoice(JlrCompareDetailActivity.this, "箱号未输入");
            return;
        }
        if (StringUtils.isEmpty(stvCustomBoxNumber.getText().toString())) {
            return;
        }
        boolean flag = false;
        for (JLRPalletBox jlrPalletBox : jlrPalletBoxList) {
            if (StringUtils.equals(jlrPalletBox.getBoxNumber(), stvBoxNumber.getText().toString()) &&
                    StringUtils.equals(jlrPalletBox.getCustomBoxNumber(), stvCustomBoxNumber.getText().toString())) {
                flag = true;
                jlrPalletBox.setChecked(true);
                break;
            }
        }
        if (!flag) {
            stvCustomBoxNumber.setText("");
            stvCustomBoxNumber.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stvCustomBoxNumber.requestFocus();
                }
            }, 200);
            MessageUtils.errorMsgAndVoice(JlrCompareDetailActivity.this, "箱标与客户箱标不匹配");
            return;
        }
        Collections.sort(jlrPalletBoxList, new Comparator<JLRPalletBox>() {
            @Override
            public int compare(JLRPalletBox t0, JLRPalletBox t1) {
                if (!t0.isChecked() && t1.isChecked()) {
                    return -1;
                }
                return 0;
            }
        });
        JlrCompareDetailAdapter jlrCompareDetailAdapter = new JlrCompareDetailAdapter(JlrCompareDetailActivity.this, jlrPalletBoxList);
        lvTaskList.setAdapter(jlrCompareDetailAdapter);

        stvBoxNumber.setText("");
        stvCustomBoxNumber.setText("");
        stvBoxNumber.postDelayed(new Runnable() {
            @Override
            public void run() {
                stvBoxNumber.requestFocus();
            }
        }, 200);
    }

    private void clearButtonMd(View v) {
        stvBoxNumber.requestFocus();
        stvCustomBoxNumber.setText("");
        stvBoxNumber.setText("");

        JlrCompareDetailAdapter jlrCompareDetailAdapter = new JlrCompareDetailAdapter(JlrCompareDetailActivity.this, jlrPalletBoxList);
        lvTaskList.setAdapter(jlrCompareDetailAdapter);
        XToastUtils.success("重置成功！");
    }

    private void SubmitButtonMd(View view) {

        boolean flag = false;
        for (JLRPalletBox jlrPalletBox : jlrPalletBoxList) {
            if (!jlrPalletBox.isChecked()) {
                flag = true;
            }
        }
        if (flag) {
            MessageUtils.errorMsgAndVoice(JlrCompareDetailActivity.this, "没有比对完成");
            return;
        }

        List<TeslaComapreInfo> teslaComapreInfoList = new ArrayList<>();
        for (JLRPalletBox jlrPalletBox : jlrPalletBoxList) {
            TeslaComapreInfo teslaComapreInfo = new TeslaComapreInfo();
            teslaComapreInfo.setCustomBoxNumber(jlrPalletBox.getCustomBoxNumber());
            teslaComapreInfo.setCustomPallet(jlrPalletBox.getCustomPallet());
            teslaComapreInfo.setBoxNumber(jlrPalletBox.getBoxNumber());
            teslaComapreInfo.setTaskNo(jlrPalletBox.getTaskNo());
            teslaComapreInfo.setMark(String.valueOf(CustomCompare.JLR.getCode()));

            teslaComapreInfoList.add(teslaComapreInfo);
        }
        TeslaCustomCompareRequest request = new TeslaCustomCompareRequest();
        request.setTeslaCustomCompares(teslaComapreInfoList);
        mMiniLoadingDialog.show();
        XHttp.post("api/Comparison/JLRCustomLabelCompare")
                .upJson(JsonUtil.toJson(request))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();
                        XToastUtils.success("比对成功", 2500);

                        setResult(0);
                        finish();
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MessageUtils.errorMsgAndVoice(JlrCompareDetailActivity.this, e);
                    }
                }) {
                });
    }
}